![]() |
Java Database Programming with JDBC
by Pratik Patel Coriolis, The Coriolis Group ISBN: 1576100561 Pub Date: 10/01/96 |
Previous | Table of Contents | Next |
Listing 10.12 Working with interfaces.
//------------------------------------------------------------------------ // MyInterface.java // // Sample code to demonstrate the use of interfaces //------------------------------------------------------------------------ package jdbc.test; public interface MyInterface { //-------------------------------------------------------------------- // Define 3 methods in this interface //-------------------------------------------------------------------- void method1(); int method2(int x); String method3(String y); } //------------------------------------------------------------------------ // MyImplementation.java // // Sample code to demonstrate the use of interfaces //------------------------------------------------------------------------ package jdbc.test; public class MyImplementation implements jdbc.test.MyInterface { //-------------------------------------------------------------------- // Implement the 3 methods in the interface //-------------------------------------------------------------------- public void method1() { } public int method2(int x) { return addOne(x); } public String method3(String y) { return y; } //-------------------------------------------------------------------- // Note that you are free to add methods and attributes to this // new class that were not in the interface, but they cannot be // seen from the interface. //-------------------------------------------------------------------- protected int addOne(int x) { return x + 1; } } //------------------------------------------------------------------------ // TestInterface.java // // Sample code to demonstrate the use of interfaces //------------------------------------------------------------------------ import jdbc.test.*; class TestInterface { public static void main (String args[]) { // Create a new MyImplementation object. We are assigning the // new object to a MyInterface variable, thus we will only be // able to use the interface methods. MyInterface myInterface = new MyImplementation(); // Call the methods myInterface.method1(); int x = myInterface.method2(1); String y = myInterface.method3("Hello, World."); } }
As you can see, implementing interfaces is easy. Well go into more detail with the major JDBC interfaces later in this chapter. But first, we need to cover some basic foundations that should be a part of every good JDBC driver.
One detail that is often overlooked by software developers is providing a facility to enable debugging. The JDBC API does provide methods to enable and disable tracing, but it is ultimately up to the driver developer to provide tracing information in the driver. It becomes even more critical to provide a detailed level of tracing when you consider the possible wide-spread distribution of your driver. People from all over the world may be using your software, and they will expect a certain level of support if problems arise. For this reason, I consider it a must to trace all of the JDBC API method calls (so that a problem can be re-created using the output from a trace).
The DriverManager provides a method to set the tracing PrintStream to be used for all of the drivers; not only those that are currently active, but any drivers that are subsequently loaded. Note that if two applications are using JDBC, and both have turned tracing on, the PrintStream that is set last will be shared by both applications. The following code snippet shows how to turn tracing on, sending any trace messages to a local file:
try { // Create a new OuputStream using a file. This may fail if the // calling application/applet does not have the proper security // to write to a local disk. java.io.OutputStream outFile = new java.io.FileOutputStream("jdbc.out"); // Create a PrintStream object using our newly created OuputStream // object. The second parameter indicates to flush all output with // each write. This ensures that all trace information gets written // into the file. java.io.PrintStream outStream = new java.io.PrintStream(outFile, true); // Enable the JDBC tracing, using the PrintStream DriverManager.setLogStream(outStream); } catch (Exception ex) { // Something failed during enabling JDBC tracing. Notify the // application that tracing is not available. . . . }
Using this code, a new file named jdbc.out will be created (if an existing file already exists, it will be overwritten), and any tracing information will be saved in the file.
The DriverManager also provides a method to write information to the tracing OutputStream. The println method will first check to ensure that a trace OutputStream has been registered, and if so, the println method of the OutputStream will be called. Heres an example of writing trace information:
// Send some information to the JDBC trace OutputStream String a = "The quick brown fox "; String b = "jumped over the "; String c = "lazy dog"; DriverManager.println("Trace=" + a + b + c);
In this example, a String message of Trace=The quick brown fox jumped over the lazy dog will be constructed, the message will be provided as a parameter to the DriverManager.println method, and the message will be written to the OutputStream being used for tracing (if one has been registered).
Some of the JDBC components are also nice enough to provide tracing information. The DriverManager object traces most of its method calls. SQLException also sends trace information whenever an exception is thrown. If you were to use the previous code example and enable tracing to a file, the following example output will be created when attempting to connect to the SimpleText driver:
DriverManager.initialize: jdbc.drivers = null JDBC DriverManager initialized registerDriver: driver[className=jdbc.SimpleText.SimpleTextDriver,context=null, jdbc.SimpleText.SimpleTextDriver@1393860] DriverManager.getConnection("jdbc:SimpleText") trying driver[className=jdbc.SimpleText.SimpleTextDriver,context=null, jdbc.SimpleText.SimpleTextDriver@1393860] driver[className=jdbc.SimpleText.SimpleTextDriver,context=null,j dbc.SimpleText.SimpleTextDriver@1393860]
Previous | Table of Contents | Next |